package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbActivityType;
import com.summer.entity.TbResponse;
import com.summer.entity.TbRole;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbResponseMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *测试完成20:41
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-response")
@Transactional
@Api(value = "TbResponseController", description = "管理员创建某个活动抢答api")

public class TbResponseController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required = false)
    TbResponseMapper tbResponseMapper;
    /**
     * is_open
     * name
     * activity_id
     * response_time
     */
    /**
     * 管理员创建活动抢答即开启抢答
     * @param activity_id
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */
    @ApiOperation("管理员创建活动抢答即开启抢答")
    @PostMapping("/teacher/addResponse")
    @ResponseBody
    public ResponseEntity addResponse(//@RequestParam(required = true) int is_open,
                                     @RequestParam(required = true) int activity_id,
                                     @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        if (tbUserinfo.getUserId()!=1&&tbUserinfo.getUserId()>0){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","权限错误");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
            responseEntity = ResponseEntity.ok();
            TbResponse tbResponse = new TbResponse();
            tbResponse.setIs_open(1);
            tbResponse.setActivityId(activity_id);
            tbResponseMapper.insert(tbResponse);
            responseEntity.putDataValue("response_id",tbResponse.getResponseId());
            responseEntity.putDataValue("msg","活动抢答添加成功");
        return responseEntity;
    }

    /**
     * 管理员关闭抢答
     * @param response_id
     *
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("管理员关闭抢答")
    @PutMapping("/web/updateTbResponse")
    @ResponseBody
    public ResponseEntity updateTbResponse(
                                        //@RequestParam(required = true) int activity_id,
                                         @RequestParam(required = false) int response_id,
                                        // @RequestParam(required = false) int is_open,
                                         @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()!=1) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            TbResponse tbResponse = tbResponseMapper.selectById(response_id);
            tbResponse.setIs_open(0);
            tbResponseMapper.updateById(tbResponse);
            responseEntity.putDataValue("msg", "关闭抢答成功");
            return responseEntity;
        }
    }

    /**
     * 活动分类列表
     * @return
     * @throws IOException
     */
    @ApiOperation("活动抢答列表")
    @PostMapping("/web/TbResponseList")
    @ResponseBody
    public ResponseEntity TbResponseList() throws IOException {

        ResponseEntity responseData;
        QueryWrapper<TbResponse> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbResponse> list = tbResponseMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }
}
